<?php

namespace App\Models;

use App\Services\ChargeService;
use App\Services\CommonService;
use App\Services\SmsService;
use App\Services\WechatService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Prophecy\Prophecy\RevealerInterface;

class ShopSms extends Model
{
    use HasFactory;
    protected $table = 'sp_shop_sms';
    protected $primaryKey = 'id';
    public $timestamps = false;


    //下单成功短信通知商户发货
    protected function orderPlaceSendToShop($seller_id,$realname,$trade_no){
        $sms_template = SmsTemplate::getSmsContent(SmsTemplate::SMS_TEMPLATE_ORDER_PLACE_SEND_SHOP);
        if($sms_template){
            //开启短信提醒
            $send_mobile = $this->where([['member_id',$seller_id],['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_PLACE_SEND_SHOP],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->value('send_mobile');
            if($send_mobile){
                $sms_content1 = str_replace('{$user_name}',$realname,$sms_template);
                $sms_content = str_replace('{$order_id}',$trade_no,$sms_content1);
                ChargeService::smsUse($seller_id,$send_mobile,$sms_content,SmsTemplate::SMS_TEMPLATE_ORDER_PLACE_SEND_SHOP,$trade_no);
            }
        }
    }

    //扣款成功
    protected function orderPaidSendToUser($seller_id,$realname,$trade_no,$paid_rent,$mobile,$order_id){
        $sms_template = SmsTemplate::getSmsContent(SmsTemplate::SMS_TEMPLATE_PAID_SEND_USER);
        if($sms_template){
            //开启短信提醒
            if(ShopSms::where([['member_id',$seller_id],['template_id',SmsTemplate::SMS_TEMPLATE_PAID_SEND_USER],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$user_name}',$realname,$sms_template);
                $sms_content2 = str_replace('{$order_id}',$trade_no,$sms_content1);
                $sms_content = str_replace('{$rent}',$paid_rent,$sms_content2);
                ChargeService::smsUse($seller_id,$mobile,$sms_content,SmsTemplate::SMS_TEMPLATE_PAID_SEND_USER,$trade_no,$order_id);
            }
        }
    }

    //订单物流签收成功
    protected function orderExpressSign($seller_id,$realname,$trade_no,$mobile){
        $sms_template = SmsTemplate::getSmsContent(SmsTemplate::SMS_TEMPLATE_EXPRESS_SIGN);
        if($sms_template){
            //开启短信提醒
            if(ShopSms::where([['member_id',$seller_id],['template_id',SmsTemplate::SMS_TEMPLATE_EXPRESS_SIGN],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$user_name}',$realname,$sms_template);
                $sms_content = str_replace('{$order_id}',$trade_no,$sms_content1);
                ChargeService::smsUse($seller_id,$mobile,$sms_content,SmsTemplate::SMS_TEMPLATE_EXPRESS_SIGN,$trade_no);
            }
        }
    }

    //短信账户余额不足
    protected function smsBalanceLess($member_id){
        $sms_template = SmsTemplate::where([['id',SmsTemplate::SMS_TEMPLATE_SMS_BALANCE_LESS],['sms_status',ChargeService::SMS_TEMPLATE_STATUS_OPEN]])->value('clsms_content');
        if($sms_template){
            //开启短信提醒
            $send_mobile = ShopSms::where([['member_id',$member_id],['template_id',SmsTemplate::SMS_TEMPLATE_SMS_BALANCE_LESS],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->value('send_mobile');
            if($send_mobile){
                ChargeService::smsUse($member_id,$send_mobile,$sms_template,SmsTemplate::SMS_TEMPLATE_SMS_BALANCE_LESS);
            }
        }
    }

    //短信自动充值失败
    protected function smsAutoChargeFail($member_id){
        $sms_template = SmsTemplate::where([['id',SmsTemplate::SMS_TEMPLATE_SMS_AUTO_CHARGE_FAIL],['sms_status',ChargeService::SMS_TEMPLATE_STATUS_OPEN]])->value('clsms_content');
        if($sms_template){
            //开启短信提醒
            $send_mobile = ShopSms::where([['member_id',$member_id],['template_id',SmsTemplate::SMS_TEMPLATE_SMS_AUTO_CHARGE_FAIL],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->value('send_mobile');
            if($send_mobile){
                ChargeService::smsUse($member_id,$send_mobile,$sms_template,SmsTemplate::SMS_TEMPLATE_SMS_AUTO_CHARGE_FAIL);
            }
        }
    }

    //自动充值(合同/短信)
    protected function autoCharge($member_id, $sms_template_id){
        $sms_content = SmsTemplate::where([['id',$sms_template_id],['sms_status',ChargeService::SMS_TEMPLATE_STATUS_OPEN]])->value('clsms_content');
        if($sms_content){
            //开启短信提醒
            $send_mobile = ShopSms::where([['member_id',$member_id],['template_id',$sms_template_id],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->value('send_mobile');
            if($send_mobile){
                ChargeService::smsUse($member_id,$send_mobile,$sms_content,$sms_template_id);
            }
        }
    }

    //订单退款成功
    protected function orderRefundSuccess($seller_id,$realname,$trade_no,$mobile,$order_id,$user_id=''){
        $sms_template = SmsTemplate::getSmsContent(SmsTemplate::SMS_TEMPLATE_ORDER_REFUND_SUCCESS);
        if($sms_template){
            //开启短信提醒
            if(ShopSms::where([['member_id',$seller_id],['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_REFUND_SUCCESS],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$user_name}',$realname,$sms_template);
                $sms_content = str_replace('{$order_id}',$trade_no,$sms_content1);
                ChargeService::smsUse($seller_id,$mobile,$sms_content,SmsTemplate::SMS_TEMPLATE_ORDER_REFUND_SUCCESS,$trade_no,$order_id,$user_id);
            }
        }
    }

    //申请退款通知商户
    protected function orderApplyRefund($seller_id,$realname,$trade_no){
        $sms_template = SmsTemplate::getSmsContent(SmsTemplate::SMS_TEMPLATE_ORDER_REFUND_SEND_SHOP);
        if($sms_template){
            //开启短信提醒
            $send_mobile = ShopSms::where([['member_id',$seller_id],['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_REFUND_SEND_SHOP],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->value('send_mobile');
            if(!empty($send_mobile)){
                $sms_content1 = str_replace('{$user_name}',$realname,$sms_template);
                $sms_content = str_replace('{$order_id}',$trade_no,$sms_content1);
                ChargeService::smsUse($seller_id,$send_mobile,$sms_content,SmsTemplate::SMS_TEMPLATE_ORDER_REFUND_SEND_SHOP,$trade_no);
            }
        }
    }

    //订单退款成功
    protected function orderDeliverySuccess($seller_id,$realname,$trade_no,$mobile,$express,$express_code){
        $sms_template = SmsTemplate::getSmsContent(SmsTemplate::SMS_TEMPLATE_ORDER_DELIVERY);
        if($sms_template){
            //开启短信提醒
            if(ShopSms::where([['member_id',$seller_id],['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_DELIVERY],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$user_name}',$realname,$sms_template);
                $sms_content2 = str_replace('{$order_id}',$trade_no,$sms_content1);
                $sms_content3 = str_replace('{$express}',$express,$sms_content2);
                $sms_content = str_replace('{$express_code}',$express_code,$sms_content3);
                ChargeService::smsUse($seller_id,$mobile,$sms_content,SmsTemplate::SMS_TEMPLATE_ORDER_DELIVERY,$trade_no);
            }
        }
    }


    //订单租金催收提醒
    protected function orderOverdueCollection($seller_id,$realname,$goods_name,$month_rent,$time,$rent_num,$trade_no,$mobile,$admin_id,$lease_id){
        $sms_template = SmsTemplate::getSmsContent(SmsTemplate::SMS_TEMPLATE_ORDER_COLLECTION);
        if($sms_template){
            //开启短信提醒
            if(ShopSms::where([['member_id',$seller_id],['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_COLLECTION],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$user_name}',$realname,$sms_template);
                $sms_content2 = str_replace('{$goods_name}',$goods_name,$sms_content1);
                $sms_content3 = str_replace('{$month_rent}',$month_rent,$sms_content2);
                $sms_content4 = str_replace('{$time}',$time,$sms_content3);
                $sms_content = str_replace('{$rent_num}',$rent_num,$sms_content4);
                $res = ChargeService::smsUse($seller_id,$mobile,$sms_content,SmsTemplate::SMS_TEMPLATE_ORDER_COLLECTION,$trade_no,$admin_id,$lease_id);
                return $res;
            }else{
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'短信未开启'];
            }
        }else{
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'短信模板不存在'];
        }
    }

    //修改账单为线下支付验证码短信
    protected function orderUpdateOfflinePay($member_id,$trade_no,$rent_num,$code){
        $sms_template = SmsTemplate::where([['id',SmsTemplate::SMS_TEMPLATE_OFFLINE_PAY_CODE],['sms_status',ChargeService::SMS_TEMPLATE_STATUS_OPEN]])->value('clsms_content');
        if($sms_template){
            //开启短信提醒
            $send_mobile = ShopSms::where([['member_id',$member_id],['template_id',SmsTemplate::SMS_TEMPLATE_OFFLINE_PAY_CODE],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])
                ->value('send_mobile');
            if($send_mobile){
                $sms_content1 = str_replace('{$order_id}',$trade_no,$sms_template);
                $sms_content2 = str_replace('{$rent_num}',$rent_num,$sms_content1);
                $sms_content3 = str_replace('{$code}',$code,$sms_content2);
                $send_res = ChargeService::smsUse($member_id,$send_mobile,$sms_content3,SmsTemplate::SMS_TEMPLATE_OFFLINE_PAY_CODE);
                return $send_res;
            }else{
                return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'短信未开启'];
            }
        }else{
            return ['status'=>CommonService::HTTP_CODE_FAIL,'msg'=>'短信模板不存在'];
        }
    }

    /**
     * 订单还款提醒
     * @param $member_id
     * @param $send_mobile
     * @param $number
     * @param $month_rent
     * @param $trade_no
     * @param $lease_id
     * @return array
     */
    public static function repaymentReminder($member_id,$send_mobile,$number,$month_rent,$trade_no,$lease_id,$date,$user_id){
        $sms_template = SmsTemplate::where([['id',SmsTemplate::SMS_TEMPLATE_REPAYMENT_REMINDER_TWO],['sms_status',ChargeService::SMS_TEMPLATE_STATUS_OPEN]])->value('clsms_content');
        if($sms_template){
            //开启短信提醒
            if(ShopSms::where([['member_id',$member_id],['template_id',SmsTemplate::SMS_TEMPLATE_REPAYMENT_REMINDER_TWO],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$number}',$number,$sms_template);
                $sms_content2 = str_replace('{$month_rent}',$month_rent,$sms_content1);
                $sms_content3 = str_replace('{$date}',$date,$sms_content2);

                    if($user_id){
                        $sms_content3 = str_replace('{$link}','https://ur.alipay.com/_7ad58J5Ga45iA0v5xFfYtw',$sms_content3);
                    }else{
                        $reslink = WechatService::getLinkUrl(0);
                        $sms_content3 = str_replace('{$link}',$reslink['url_link'],$sms_content3);
                    }

                    $send_res = ChargeService::smsUse($member_id,$send_mobile,$sms_content3,SmsTemplate::SMS_TEMPLATE_REPAYMENT_REMINDER_TWO,$trade_no,1,$lease_id);

                return $send_res;
            }
        }
    }

    /**
     * 账单扣款失败提醒
     * @param $member_id
     * @param $send_mobile
     * @param $number
     * @param $month_rent
     * @param $trade_no
     * @param $lease_id
     * @return array
     */
    public static function withholdFail($member_id,$send_mobile,$number,$month_rent,$trade_no,$lease_id){
        $sms_template = SmsTemplate::where([['id',SmsTemplate::SMS_TEMPLATE_WITHHOLD_FAIL],['sms_status',ChargeService::SMS_TEMPLATE_STATUS_OPEN]])->value('clsms_content');
        if($sms_template){
            //开启短信提醒
            if(ShopSms::where([['member_id',$member_id],['template_id',SmsTemplate::SMS_TEMPLATE_WITHHOLD_FAIL],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$number}',$number,$sms_template);
                $sms_content2 = str_replace('{$month_rent}',$month_rent,$sms_content1);
                $send_res = ChargeService::smsUse($member_id,$send_mobile,$sms_content2,SmsTemplate::SMS_TEMPLATE_WITHHOLD_FAIL,$trade_no,1,$lease_id);
                return $send_res;
            }
        }
    }

    /**
     * 订单逾期短信提醒
     * @param $member_id
     * @param $send_mobile
     * @param $overdue_amount
     * @param $trade_no
     * @return array
     */
    public static function overdueReminder($member_id,$send_mobile,$overdue_amount,$trade_no){
        $sms_template = SmsTemplate::where([['id',SmsTemplate::SMS_TEMPLATE_ORDER_OVERDUE],['sms_status',ChargeService::SMS_TEMPLATE_STATUS_OPEN]])->value('clsms_content');
        if($sms_template){
            //开启短信提醒
            if(ShopSms::where([['member_id',$member_id],['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_OVERDUE],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$month_rent}',$overdue_amount,$sms_template);

                    $send_res = ChargeService::smsUse($member_id, $send_mobile, $sms_content1, SmsTemplate::SMS_TEMPLATE_ORDER_OVERDUE, $trade_no);
                return $send_res;
            }
        }
    }



    /**
     * 订单逾期超15天短信提醒
     * @param $member_id
     * @param $send_mobile
     * @param $overdue_amount
     * @param $trade_no
     * @return array
     */
    public static function overdueReminderBeyond($member_id,$send_mobile,$trade_no,$user_name,$begin_time,$end_time){
        $sms_template = SmsTemplate::where([['id',SmsTemplate::SMS_TEMPLATE_ORDER_OVERDUE_BEYOND],['sms_status',ChargeService::SMS_TEMPLATE_STATUS_OPEN]])->value('clsms_content');
        if($sms_template){
            //开启短信提醒
//            if(ShopSms::where([['member_id',$member_id],['template_id',SmsTemplate::SMS_TEMPLATE_ORDER_OVERDUE_BEYOND],['status',ChargeService::SHOP_SMS_STATUS_OPEN]])->count() > 0){
                $sms_content1 = str_replace('{$user_name}',$user_name,$sms_template);
                $sms_content2 = str_replace('{$begin_time}',$begin_time,$sms_content1);
                $sms_content3 = str_replace('{$end_time}',$end_time,$sms_content2);
                $sms_content4 = str_replace('{$time}',date('Y年m月d日'),$sms_content3);

                $send_res = ChargeService::smsUse($member_id, $send_mobile, $sms_content4, SmsTemplate::SMS_TEMPLATE_ORDER_OVERDUE_BEYOND, $trade_no);
                return $send_res;
//            }
        }
    }



    //订单物流签收成功
    protected function orderAuditSms($seller_id,$trade_no,$mobile){
        $sms_template = SmsTemplate::getSmsContent(SmsTemplate::SMS_TEMPLATE_REPAYMENT_ORDER_AUDIT);
        if($sms_template){
           $res=ChargeService::smsUse($seller_id,$mobile,$sms_template,SmsTemplate::SMS_TEMPLATE_REPAYMENT_ORDER_AUDIT,$trade_no);
        }
        }

    protected function GetOneShopSms($where)
    {
        $data = $this
            ->where($where)
            ->first();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //添加
    protected function addData($data)
    {
        return $data = $this->insert($data);
    }

    protected function editData($where, $data)
    {
        return $data = $this->where($where)->update($data);
    }


}
